package vista;

import java.awt.Color;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import constants.Constants;


/**
 * this class will let us to create a panel to exchange cards between users  
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class PlayCardFrame extends JDialog implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private DrawingPanel panel;
	private JButton closeButton;
	private JButton sendButton;

	
	private List<Image> monkImages;
	private boolean[] monkImagesToShow;
	
	
	private JComboBox<String> features;
	private JComboBox<String> destiny;
	private JComboBox<String> players1;
	private JComboBox<String> players2;
	private List<String> questions;
	
	private JLabel title;
	
	private String cardName;
	
	private static final int cardWidth = 87;
	private static final int cardHeight = 160;
	
	/**
	 * this construct creates our exchange Card zone 
	 * @param bounds the size 
	 * @param questions features to ask
	 * @param monkImagesParam the pointer to our monk card list 
	 */
	public PlayCardFrame( Rectangle bounds,List<String> questions,final List<Image> monkImagesParam,List<String> playerNames,String thisPlayerName) {
		
		this.setBounds(bounds);
		this.setLayout(null);
		this.setUndecorated(true);
		
		JPanel mainPanel = (JPanel)getContentPane();
		mainPanel.setBorder(Constants.doubleBorder);
		mainPanel.setBackground(Color.GRAY);
		
		this.monkImages = new ArrayList<Image>();
		monkImagesToShow = new boolean[monkImagesParam.size()];
		scaleImages(monkImagesParam);
		
		panel = new DrawingPanel();
		panel.setBorder(Constants.doubleBorder);
		panel.setBounds(10, 110, 600, 470);
		panel.setBackground(Color.DARK_GRAY);
		panel.setVisible(false);
		add(panel);
		
		this.questions = questions;
		features = new JComboBox<String>();
		features.setBounds(280, 70, 120, 20);
		setFeatures();
		features.setVisible(false);
		
		destiny = new JComboBox<String>();
		destiny.setBounds(280, 70, 120, 20);
		destiny.addItem("Confesionario1");
		destiny.addItem("Confesionario2");
		destiny.addItem("Cripta");
		destiny.setVisible(false);
		
		configPlayerNames(playerNames,thisPlayerName);
		
		title = new JLabel();
		title.setBounds(getWidth()/2-350/2,6,350,25);
		title.setOpaque(false);
		title.setHorizontalAlignment(SwingConstants.CENTER);
		title.setHorizontalTextPosition(SwingConstants.CENTER);
		title.setFont(Constants.BIG_FONT);
		title.setForeground(Color.BLACK);
		
		sendButton = new JButton("JUGAR CARTA");
		sendButton.setName("playCardButton");
		sendButton.setSize(120, 20);
		sendButton.setLocation(420, 70);
		sendButton.addActionListener(MainGUI.getInstance());
		
		closeButton = new JButton("X");
		closeButton.setFont(Constants.NORMAL_FONT);
		closeButton.setBackground(Color.BLACK);
		closeButton.setForeground(Color.RED);
		closeButton.setSize(70, 20);
		closeButton.setLocation(getWidth()/2 - closeButton.getWidth()/2, getHeight() - closeButton.getHeight()-10);
		closeButton.addActionListener(this);
		
		JTextArea tipAboutThis = new JTextArea();
		tipAboutThis.setBounds(10,34,600,30);
		tipAboutThis.setOpaque(false);
		tipAboutThis.setEditable(false);
		tipAboutThis.setWrapStyleWord(true);
		tipAboutThis.setLineWrap(true);
		tipAboutThis.setFont(Constants.SMALL_FONT);
		tipAboutThis.setText("NOTA: Este panel sirve para jugar las cartas de Scriptorium y Biblioteca, para algunas apareceran selectores, algunas mostraran resultados sobre cartas de sospechoso.");
		
		add(tipAboutThis);
		add(title);
		add(features);
		add(destiny);
		add(closeButton);
		add(sendButton);
		setVisible(false);
	}
	
	/**
	 * this will add the features to the current combo
	 */
	private void configPlayerNames(List<String> playerNames, String thisPlayerName) {

		players1 = new JComboBox<String>();
		players2 = new JComboBox<String>();
		
		players1.setBounds(280, 70, 120, 20);
		players1.setVisible(false);
		
		players2.setBounds(280, 95, 120, 20);
		players2.setVisible(false);
		
		for(String name:playerNames) {
			if(!name.equalsIgnoreCase(thisPlayerName)) {
				players1.addItem(name);
				players2.addItem(name);
			}
		}
		add(players1);
		add(players2);
	}


	private void setFeatures() {
		for (String pibot : questions) {
			System.err.println(pibot);
			if (!pibot.equalsIgnoreCase("titulo")
					&& !pibot.equalsIgnoreCase("cabeza")
					&& !pibot.equalsIgnoreCase("cuerpo")
					&& !pibot.equalsIgnoreCase("orden")
					&& !pibot.equalsIgnoreCase("vellofacial")
					&& !pibot.equalsIgnoreCase("|"))
				features.addItem(pibot);
		}
	}
		
	/**
	 * return the name of the card
	 * @return cardName
	 */
	public String getCardName() {
		return cardName;
	}
	/**
	 * will set a card to be added to the panel
	 * @param cardName
	 */
	public void playCard(String cardName) {
		
		this.cardName = cardName;
		title.setText(cardName.toUpperCase());
		sendButton.setEnabled(true);
		panel.setVisible(false);
		closeButton.setVisible(false);
		setVisible(true);
		
		switch(cardName.toLowerCase()) {
		
		//<CARTAS BIBLIoTECA>
		case "demonologia" :
			demonologiaOptions();
			break;
		
		case "espejo del mundo" :
			espejoDelMundoOptions();
			break;
		
		
		case "evangelio de judas" :
			evangelioDeJudasOptions();
		break;
		
		case "la risa de aristoteles": 
			onlySendButtonOptions();
		break;
		
		case "horus aploo hieroglyphicae":
			onlySendButtonOptions();
		break;
		//</CARTAS BIBLIoTECA>
		
		//<CARTAS SCRIPTORIUM>
		case "cantar de los cantares":
			onlySendButtonOptions();
			break;
			
		case "cronica de san teodulo":
			onlySendButtonOptions();
			break;
		
		case "cronica del monasterio":
			onlySendButtonOptions();
			break;
			
		case "texto criptografico":
			textoCriptograficoOptions();
			break;
			
		case "tratado astrologico arabe":
			tratadoAstrologicoOptions();
			break;
		case "tratado neoplatonico":
			evangelioDeJudasOptions();
			break;
		default : setVisible(false);
		break;
		////</CARTAS SCRIPTORIUM>
		
		}
		
	
	}
	
	private void tratadoAstrologicoOptions() {
		features.setVisible(false);
		destiny.setVisible(false);
		players1.setVisible(true);
		players2.setVisible(true);
	}


	private void textoCriptograficoOptions() {
		destiny.setVisible(true);
		features.setVisible(false);
		closeButton.setVisible(true);
		players1.setVisible(false);
		players2.setVisible(false);
	}


	private void onlySendButtonOptions() {
		features.setVisible(false);
		destiny.setVisible(false);
		closeButton.setVisible(true);
		players1.setVisible(false);
		players2.setVisible(false);
	}
	
	private void evangelioDeJudasOptions() {
		features.setVisible(false);
		destiny.setVisible(false);
		players1.setVisible(false);
		players2.setVisible(false);
	}

	private void espejoDelMundoOptions() {
		features.setVisible(false);
		destiny.setVisible(false);
		players1.setVisible(false);
		players2.setVisible(false);
	}
	/**
	 * this will handle a demonologia card comand
	 */
	private void demonologiaOptions() {
		features.setVisible(true);
		destiny.setVisible(false);
		players1.setVisible(false);
		players2.setVisible(false);
	}
	
	
	public void disableSendButton() {
		sendButton.setEnabled(false);
	}
	/**
	 * this will return our selected feature
	 * @return
	 */
	public String getSelectedFeature() {
		return (String)features.getSelectedItem();
	}
	/**
	 * this will add a card to the panel 
	 * @param infoList
	 */
	public String getSelectedDestiny() {
		return (String)destiny.getSelectedItem();
	}
	
	public String getSelectedPlayer1() {
		return (String)players1.getSelectedItem();
	}
	
	public String getSelectedPlayer2() {
		return (String)players2.getSelectedItem();
	}
	
	public void showThisCards(List<String> infoList) {
		panel.setVisible(true);
		closeButton.setVisible(true);
		for(int i=0;i<infoList.size();i++) {
			if(infoList.get(i).equals("1"))
				monkImagesToShow[i] = true;
		}
		panel.repaint();
	}
	
	

	/**
	 * this will hide our panel for the next action
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		setVisible(false);
	}
	
	/**
	 *this method will re scale the image for our panel size  
	 * @param monkImagesParam list of image to be re scaled
	 */
	private void scaleImages(final List<Image> monkImagesParam) {
		if(monkImagesParam!=null) 
			for(Image image:monkImagesParam) 
				monkImages.add(image.getScaledInstance(87, 160, BufferedImage.SCALE_SMOOTH));
	}
	
	/**
	 * this private class will create the inner zone for the cards 
	 * 
	 *
	 */
	private class DrawingPanel extends JPanel {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public DrawingPanel() { }
		
		public void paintComponent(Graphics g) {

			g.setColor(Color.DARK_GRAY);
			g.fillRect(0, 0, getWidth(), getHeight());
			int j = 0;
			for (int i =0 ; i < monkImages.size();i++) {
				int posX = (j % 6) * (10 + cardWidth)+ 13;
				int posY = (j / 6) * (10 + cardHeight)+ 13;
				if(monkImagesToShow[i]) {
					j++;
					g.drawImage(monkImages.get(i), posX, posY, cardWidth, cardHeight,null);
				}
			}	
		}
		
	}
}
